package com.tuling.jucdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 模拟减库存操作
 */
@RestController
public class StockController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/reuce_stock")
    public String reduceStock(){
        //reentrantLock
        //查库存
        int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
        if(stock>0){
            stock--;
            stringRedisTemplate.opsForValue().set("stock", stock+",");
            System.out.println("扣减库存成功,库存stock:" + stock);
        }else {
            System.out.println("扣减库存失败,库存不做");
        }
        return "end";
    }

}
